package company.permissionsystem.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import company.permissionsystem.model.entity.Department;
import company.permissionsystem.model.entity.SysUser;
import company.permissionsystem.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @GetMapping("/department")
    public String departmentIndex(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum, Model model) {
        PageHelper.startPage(pageNum, 8);
        List<Department> departmentList = departmentService.findAllDepartment();
        PageInfo<Department> lists = new PageInfo<>(departmentList);
        model.addAttribute("departments", lists);
        return "department/department";
    }

    @GetMapping("/department/{id}")
    public String findDepartmentEmployee(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                                  Model model,@PathVariable Long id) {
        PageHelper.startPage(pageNum, 8);
        List<SysUser> sysUsers = departmentService.findEmployeeByDepartmentId(id);
        PageInfo<SysUser> lists = new PageInfo<>(sysUsers);
        model.addAttribute("users", lists);
        return "employee/employee-list";
    }

}
